import { isObject } from './util'

function normalizeHeaderName(headers: any, normalizeName: string): void {
  if (!headers) return
  Object.keys(headers).forEach((name: string) => {
    if (name !== normalizeName && name.toUpperCase() === normalizeName) {
      headers[normalizeName] = headers[name]
      delete headers[name]
    }
  })
  return headers
}

export function processRequestHeader(headers: any = {}, data: Object) {
  // 规范化content-type名称
  normalizeHeaderName(headers, 'Content-Type')

  if (isObject(data)) {
    if (headers && !headers['Content-Type']) {
      headers['Content-Type'] = 'application/json;charset=utf-8'
    }
  }

  return headers
}
